package aimis.service.impl;

import aimis.domain.Monitor;
import aimis.domain.PlantingRecords;
import aimis.domain.Region;
import aimis.domain.TobaccoCrops;
import aimis.repo.AreaMapper;
import aimis.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    AreaMapper areaMapper;
    @Override
    public Region IdFindRegion(int id) {
        return areaMapper.IdFindRegion(id);
    }

    @Override
    public TobaccoCrops IdFindCrops(int id) {
        return areaMapper.IdFindCrops(id);
    }

    @Override
    public List<PlantingRecords> RegIdFind(int region_id) {
        List<PlantingRecords> plantingRecords = areaMapper.RegIdFind(region_id);
        for(PlantingRecords x : plantingRecords){
            Region records = IdFindRegion(x.getRegionid());
            TobaccoCrops tobaccoCrops = IdFindCrops(x.getCropid());
            x.setRName(records.getName());
            x.setPlantedarea(records.getPlantedarea());
            x.setUnplantedarea(records.getUnplantedarea());
            x.setName(tobaccoCrops.getName());
            x.setGrowthcycle(tobaccoCrops.getGrowthcycle());
            x.setPhotourl(tobaccoCrops.getPhotourl());
        }
        return plantingRecords;
    }

    @Override
    public List<PlantingRecords> SowingDateFind(Date sowing_date) {
        List<PlantingRecords> plantingRecords = areaMapper.SowingDateFind(sowing_date);
        for( PlantingRecords x : plantingRecords){
            Region records = IdFindRegion(x.getRegionid());
            TobaccoCrops tobaccoCrops = IdFindCrops(x.getCropid());
            x.setRName(records.getName());
            x.setPlantedarea(records.getPlantedarea());
            x.setUnplantedarea(records.getUnplantedarea());
            x.setName(tobaccoCrops.getName());
            x.setGrowthcycle(tobaccoCrops.getGrowthcycle());
            x.setPhotourl(tobaccoCrops.getPhotourl());
        }
        return plantingRecords;
    }

    @Override
    public void InsertAres(String name, String planted_area, String unplanted_area) {
        areaMapper.InsertAres(name,planted_area,unplanted_area);
    }


    @Override
    public void InsertTobacco(String name, String growth_cycle, String photo_url) {
        areaMapper.InsertTobacco(name,growth_cycle,photo_url);
    }

    @Override
    public void InsertPlanting(int region_id, int crop_id, float planting_area, Date sowing_date, Date harvest_date, int number) {
        areaMapper.InsertPlanting(region_id,crop_id,planting_area,sowing_date,harvest_date,number);
    }

    @Override
    public List<PlantingRecords> getAllSearch(String keyWord){
        List<PlantingRecords> plantingRecords = areaMapper.getAllSearch();
        List<PlantingRecords> x1 = new ArrayList<>();
        for (PlantingRecords x : plantingRecords) {
            String resultString = x.getId() + "," +
                    x.getSowingdate() + "," +
                    x.getHarvestdate() + "," +
                    x.getPlantingarea() + "," +
                    x.getRName() + "," +
                    x.getPlantedarea() + "," +
                    x.getUnplantedarea() + "," +
                    x.getName() + "," +
                    x.getGrowthcycle() + "," +
                    x.getPhotourl();
            if(resultString.contains(keyWord)){
                x1.add(x);
            }
        }
        return x1;
    }

    @Override
    public void deletePlantingRecordById(Long id) {
        PlantingRecords record = areaMapper.findById(id);
        if (record == null) {
            throw new RuntimeException("Record not found");
        }else{
        Long regionId = (long) record.getRegionid();
        Long cropId = (long) record.getCropid();
        areaMapper.deleterById(regionId);
        areaMapper.deleteCById(cropId);
        areaMapper.deleteById(id);
    }
    }


    //根据id查询大表在那一字段
    @Override
    public PlantingRecords IdFindPlanting(int id) {
        return areaMapper.IdFindPlanting(id);
    }

    @Override
    public void updateTobacco(String name, String growth_cycle, String photo_url, int crop_id) {
        areaMapper.updateTobacco(name,growth_cycle,photo_url,crop_id);
    }

    @Override
    public void updateAres(String name, String planted_area, String unplanted_area, int region_id) {
        areaMapper.updateAres(name,planted_area,unplanted_area,region_id);
    }
}

